package com.oneguy;

import com.oneguy.model.bean.ActiveUser;
import com.oneguy.model.bean.RevenueScore;
import com.oneguy.model.bean.RoomInfoEntity;

import java.util.*;

/**
 * Created by ZuoShu on 16/12/6.
 */
public class Cache {
    private static final String TAG = "Cache";
    private static Cache instance;

    private Map<String, RoomInfoEntity> roomMap;

    private long lastReadRoomTime;

    private List<RevenueScore> revenueScoreList;

    private List<ActiveUser> activeUserList;

    // Map<command,Set<username>>
    private Map<String, Set<String>> monitorMap;

    public static synchronized Cache getInstance() {
        if (instance == null) {
            instance = new Cache();
        }
        return instance;
    }

    public Cache() {
        roomMap = new HashMap<String, RoomInfoEntity>();
        revenueScoreList = new ArrayList<RevenueScore>();
        monitorMap = new HashMap<String, Set<String>>();
        activeUserList = new ArrayList<ActiveUser>();
    }

    public void addMonitor(String command, String username) {
        synchronized (monitorMap) {
            Set<String> userSet = null;
            if (monitorMap.containsKey(command)) {
                userSet = monitorMap.get(command);
            } else {
                userSet = new HashSet<String>();
            }
            userSet.add(username);
            monitorMap.put(command, userSet);
        }
    }

    public void removeMonitor(String command, String username) {
        synchronized (monitorMap) {
            if (monitorMap.containsKey(command)) {
                Set<String> userSet = monitorMap.get(command);
                userSet.remove(username);
                monitorMap.put(command, userSet);
            }
        }
    }

    public boolean hasMonitor(String command, String username) {
        synchronized (monitorMap) {
            return monitorMap.containsKey(command)
                    && monitorMap.get(command).contains(username);
        }
    }

    public Set<String> getMonitorUser(String command) {
        return monitorMap.get(command);
    }

    public List<ActiveUser> getActiveUserList() {
        return activeUserList;
    }

    public void setActiveUserList(List<ActiveUser> activeUserList) {
        this.activeUserList = activeUserList;
    }

    public List<RevenueScore> getRevenueScoreList() {
        return revenueScoreList;
    }

    public void setRevenueScoreList(List<RevenueScore> revenueScoreList) {
        this.revenueScoreList = revenueScoreList;
    }

    public void setRoom(RoomInfoEntity room) {
        if (room == null) {
            return;
        }

        String key = getRoomKey(room);
        roomMap.put(key, room);
    }

    public boolean hasRoom(RoomInfoEntity room) {
        return room != null && roomMap.containsKey(getRoomKey(room));
    }

    private String getRoomKey(RoomInfoEntity roomInfoEntity) {
        if (roomInfoEntity == null) {
            return "";
        }
        return getRoomKey(String.valueOf(roomInfoEntity.getRoomGuid())
                , String.valueOf(roomInfoEntity.getFirstTableGuid()));
    }

    public List<RoomInfoEntity> getRooms() {
        List<RoomInfoEntity> rooms = new ArrayList<RoomInfoEntity>();
        rooms.addAll(roomMap.values());
        return rooms;
    }

    private String getRoomKey(String guid, String firstTableGuid) {
        return guid + firstTableGuid;
    }

    public void setRoom(String roomId, String firstTable, String factor) {
        String key = getRoomKey(roomId, firstTable);
        if (!roomMap.containsKey(key)) {
            return;
        }
        RoomInfoEntity roomInfo = roomMap.get(key);
        String oldFactor = roomInfo.getFactor();
        roomInfo.setFactor(factor);
        roomMap.put(key, roomInfo);
        Log.i(TAG, "Room id:" + roomId + " table:" + firstTable + " " + oldFactor + " -> " + factor);
    }

    public long getLastReadRoomTime() {
        return lastReadRoomTime;
    }

    public void setLastReadRoomTime(long lastReadRoomTime) {
        this.lastReadRoomTime = lastReadRoomTime;
    }
}
